import {createStore} from 'vuex';
import createPersistedState from "vuex-persistedstate"
import Cookie from "js-cookie";

export default createStore({
    //存储状态
    state: {
        loginUserInfo: null,
        token: null
    },
    //改变状态
    mutations: {
        setLoginUserInfo(state, loginUserInfo) {
            state.loginUserInfo = loginUserInfo;
        },
        setToken(state, token) {
            state.token = token;
        }
    },
    //提交状态改变
    actions: {
        setLoginUserInfo({commit}, loginUserInfo) {
            commit("setLoginUserInfo", loginUserInfo);
        },
        setToken({commit}, token) {
            commit("setToken", token);
        },
        //退出登录，把之前保存的登录信息去掉
        setLogOut({commit}) {
            commit("setLoginUserInfo", null);
            commit("setToken", null);
        }
    },
    getters: {
        loginUserInfo(state) {
            return state.loginUserInfo;
        },
        token(state) {
            return state.token
        }
    },
    plugins: [
        //这里可以加载很多插件，如异步转同步插件，如持久化插件
        createPersistedState({
            storage: {
                getItem: key => Cookie.get(key),
                setItem: ((key, value) => Cookie.set(key, value, {expires: 30})),
                removeItem: key => Cookie.remove(key)
            }
        })
    ]
})
